/* 
 -----------------------------------------------------------------------------
                   Cogaen - Component-based Game Engine (v3)
 -----------------------------------------------------------------------------
 This software is developed by the Cogaen Development Team. Please have a 
 look at our project home page for further details: http://www.cogaen.org
    
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Copyright (c) Roman Divotkey, 2010-2011

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
package org.cogaen.task;

import org.cogaen.core.Core;
import org.cogaen.event.Event;
import org.cogaen.event.EventManager;

/**
 * Fires a certain event after a specified amount of time.
 * This task can be used to schedule the dispatch of events.
 * 
 * <hr>
 * This example attaches a new task that will fire a event after five seconds.
 * 
 * <pre>
 * EndOfSplashScreenEvent endOfSplashEvt = new EndOfSplashScreenEvent();
 * FireEventTask myTask = new FireEventTask(core, endOfSplashEvt, 5.0);
 * TaskManager.getInstance(core).attachTask( myTask );
 * </pre>
 * 
 * or even shorter:
 * 
 * <pre>
 * TaskManager.getInstance(core).attachTask( new FireEventTask(core, new EndOfSplashScreenEvent(), 5.0) );
 * </pre>
 * 
 * @see TaskManager
 */
public class FireEventTask extends DelayedExecutionTask {

	/** Event to be fired after delay time. */
	private Event event;
	
	/** Indicates if this task should be restarted and run endlessly. */
	private boolean repeat;
			
	/**
	 * Creates a new instance of this task.
	 * 
	 * @param core reference to cogaen core.
	 * @param name {@code String} specifying a unique name for this task.
	 * @param event {@code Event} to be fired after specified delay time.
	 * @param delay delay time in seconds.
	 * @param repeat indicates if this task should be restarted or die after specified delay time.
	 */
	public FireEventTask(Core core, String name, Event event, double delay, boolean repeat) {
		super(core, name, delay);
		this.event = event;
		this.repeat = repeat;
	}
	
	/**
	 * Creates a new instance of this task. 
	 * {@link NameService} will be used to generate a name unique name for this task.
	 * 
	 * @param core reference to cogaen core.
	 * @param event {@code Event} to be fired after specified delay time.
	 * @param delay delay time in seconds.
	 * @param repeat indicates if this task should be restarted or die after specified delay time.
	 */
	public FireEventTask(Core core, Event event, double delay, boolean repeat) {
		super(core, delay);
		this.event = event;
		this.repeat = repeat;
	}
	
	/**
	 * Creates a new instance of this task that will end after the specified delay time.
	 * 
	 * @param core reference to cogaen core.
	 * @param name {@code String} specifying a unique name for this task.
	 * @param event {@code Event} to be fired after specified delay time.
	 * @param delay delay time in seconds.
	 */
	public FireEventTask(Core core, String name, Event event, double delay) {
		this(core, name, event, delay, false);
	}
	
	/**
	 * Creates a new instance of this task that will end after the specified delay time.
	 * {@link NameService} will be used to generate a name unique name for this task.
	 * 
	 * @param core reference to cogaen core.
	 * @param event {@code Event} to be fired after specified delay time.
	 * @param delay delay time in seconds.
	 */
	public FireEventTask(Core core, Event event, double delay) {
		this(core, event, delay, false);
	}
	
	/**
	 * Used to query if this task will be restarted or die after delay time.
	 * 
	 * @return {@code true} if this task will be restarted, {@code false} otherwise.
	 */
	public boolean isRepeat() {
		return this.repeat;
	}

	/**
	 * Determines the behavior of this task when the specified event has been fired.
	 * 
	 * @param repeat {@code true} if this task should restart after delay time, {@code false} otherwise.
	 */
	public void setRepeat(boolean repeat) {
		this.repeat = repeat;
	}
		
 	@Override
	public boolean execute() {
		EventManager.getInstance( getCore() ).enqueueEvent( this.event );
		return this.repeat;
	}
}
